package com.chen.bq;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

// 同步队列
public class SynchronizedQueueTest {
    public static void main(String[] args) throws InterruptedException {

        // 没有容量，只能一进一出
        BlockingQueue<String> blockingQueue = new SynchronousQueue<>();

        // 添加
        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName() + "添加了a" );
                blockingQueue.put("a");
                System.out.println(Thread.currentThread().getName() + "添加了b" );
                blockingQueue.put("b");
                System.out.println(Thread.currentThread().getName() + "添加了c" );
                blockingQueue.put("c");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "T1").start();

        // 释放
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName()+ "释放了" + blockingQueue.take());
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName()+ "释放了" + blockingQueue.take());
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName()+ "释放了" + blockingQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "T2").start();

    }
}
